package com.power.controller;

import com.alibaba.fastjson.JSONObject;
import com.power.common.Contants;
import com.power.ov.ResultData;
import com.power.pojo.Users;
import com.power.service.LoginService;
import com.power.service.impl.LoginServiceImpl;
import com.power.utils.WriterUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/03 8:20
 * Description:登录
 * @author 浪人
 * Version: V1.0
 */
@WebServlet("/login.do")
public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        BufferedReader br=req.getReader();
        String str=br.readLine();
        Users users= JSONObject.parseObject(str,Users.class);
        LoginService loginService=new LoginServiceImpl();
        ResultData resultData= new ResultData();
        try {
            resultData = loginService.login(users);
        } catch (SQLException e) {
            resultData.setCode(Contants.EXCEPTION);
            resultData.setMsg("系统开小差了....");
            e.printStackTrace();
        }
        //获得session后面做拦截
        if (resultData.getData()!=null){
            HttpSession session=req.getSession();
            session.setAttribute("user",resultData.getData());
        }
        WriterUtil.getWriter(resp,resultData);
        br.close();
    }
}
